<?php 
class bannersDAL 
{
	public function Insert($db, $bannersObj, &$errorMsg) 
	{
		$errorMsg = "";

		$query = "INSERT INTO banners (name, description, image, popImage, popWidth, popHeight, linkUrl, urlForView, urlForClick, webpageID, delayPeriod, startDate, endDate, price, deleteFlag) VALUES (".ToSQLPostVAL($bannersObj->name).", ".ToSQLPostVAL($bannersObj->description).", ".ToSQLPostVAL($bannersObj->image).", ".ToSQLPostVAL($bannersObj->popImage).", ".ToSQLPostVAL($bannersObj->popWidth).", ".ToSQLPostVAL($bannersObj->popHeight).", ".ToSQLPostVAL($bannersObj->linkUrl).", ".ToSQLPostVAL($bannersObj->urlForView).", ".ToSQLPostVAL($bannersObj->urlForClick).", ".ToSQLPostVAL($bannersObj->webpageID).", ".ToSQLPostVAL($bannersObj->delayPeriod).", ".ToSQLPostVAL($bannersObj->startDate).", ".ToSQLPostVAL($bannersObj->endDate).", ".ToSQLPostVAL($bannersObj->price).", ".ToSQLPostVAL($bannersObj->deleteFlag).")";
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}

		$bannersObj->id = mysql_insert_id();
		return true;
	}

	public function Update($db, $bannersObj, &$errorMsg)
	 {	
		$errorMsg = "";

		$query = "UPDATE banners SET name=".ToSQLPostVAL($bannersObj->name).", description=".ToSQLPostVAL($bannersObj->description).", image=".ToSQLPostVAL($bannersObj->image).", popImage=".ToSQLPostVAL($bannersObj->popImage).", popWidth=".ToSQLPostVAL($bannersObj->popWidth).", popHeight=".ToSQLPostVAL($bannersObj->popHeight).", linkUrl=".ToSQLPostVAL($bannersObj->linkUrl).", urlForView=".ToSQLPostVAL($bannersObj->urlForView).", urlForClick=".ToSQLPostVAL($bannersObj->urlForClick).", webpageID=".ToSQLPostVAL($bannersObj->webpageID).", delayPeriod=".ToSQLPostVAL($bannersObj->delayPeriod).", startDate=".ToSQLPostVAL($bannersObj->startDate).", endDate=".ToSQLPostVAL($bannersObj->endDate).", price=".ToSQLPostVAL($bannersObj->price).", deleteFlag=".ToSQLPostVAL($bannersObj->deleteFlag)." WHERE id=".ToSQLPostVAL($bannersObj->id);
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}

		return true;
	}

	public function Load($db, $id, &$errorMsg, $joinTables="", $onFields="", $getFields="")
	{
		$errorMsg = "";
		$joinClause = "";
		$extraFields = "";
		for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
		{
			$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
			$fields = explode(",", $getFields[$i]);
			for ($j=0; $j<count($fields); $j++)
				$extraFields .= ", ".$joinTables[$i].".".$fields[$j]." AS ".$joinTables[$i]."_".$fields[$j];
		}
		$query	= "SELECT banners.*$extraFields FROM banners $joinClause WHERE banners.id=".ToSQLPostVAL($id);

		if (!$db->query($query)) {
			$errorMsg	= mysql_error()." Query: $query.";
			return null;
		} else {
			if ($db->next_record()) {
				$bannersObj = new bannersObj($db->f("id"), $db->f("name"), $db->f("description"), $db->f("image"), $db->f("popImage"), $db->f("popWidth"), $db->f("popHeight"), $db->f("linkUrl"), $db->f("urlForView"), $db->f("urlForClick"), $db->f("webpageID"), $db->f("delayPeriod"), $db->f("startDate"), $db->f("endDate"), $db->f("price"), $db->f("deleteFlag"));
				for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
				{
					$fields = explode(",", $getFields[$i]);
					for ($j=0; $j<count($fields); $j++)
					{
						$categorieschildObj->otherFields[$joinTables[$i]."_".$fields[$j]] = $db->f($joinTables[$i]."_".$fields[$j]);
					}
				}
				return $bannersObj;
			} else {
				$errorMsg = "Data not found.";
				return null;
			}
		}
		return null;
	}

	public function IsExist($db, $whereStr) 
	{
		$query	= "SELECT * FROM banners";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else return false;

		if (!$db->query($query)) {
			return false;
		} else {
			while ($db->next_record()) {
				return true;
			}
		}
		return false;
	}

	public function Pure($db, $whereStr, &$errorMsg) 
	{
		$query	= "DELETE FROM banners";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else {
			$errorMsg = "You have no delete condition in query string ($query WHERE ?).";
			return false;
		}

		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}
		return true;
	}

	public function GetList($db, $whereStr, $orderByStr, $limit, &$errorMsg, $joinTables="", $onFields="", $getFields="") 
	{
		$returnData = array();
		$joinClause = "";
		$extraFields = "";
		for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
		{
			$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
			$fields = explode(",", $getFields[$i]);
			for ($j=0; $j<count($fields); $j++)
				$extraFields .= ", ".$joinTables[$i].".".$fields[$j]." AS ".$joinTables[$i]."_".$fields[$j];
		}
		$errorMsg = "";
		$query	= "SELECT banners.*$extraFields FROM banners$joinClause";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		if ($orderByStr!="") $query = $query." ORDER BY ".$orderByStr;
		if ($limit!="") $query = $query." LIMIT ".$limit;

		if (!$db->query($query)) {
			$errorMsg	= mysql_error()." Query: $query.";
		} else {
			while ($db->next_record()) 	{
				$objVal = new bannersObj($db->f("id"), $db->f("name"), $db->f("description"), $db->f("image"), $db->f("popImage"), $db->f("popWidth"), $db->f("popHeight"), $db->f("linkUrl"), $db->f("urlForView"), $db->f("urlForClick"), $db->f("webpageID"), $db->f("delayPeriod"), $db->f("startDate"), $db->f("endDate"), $db->f("price"), $db->f("deleteFlag"));
				for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
				{
					$fields = explode(",", $getFields[$i]);
					for ($j=0; $j<count($fields); $j++)
					{
						$objVal->otherFields[$joinTables[$i]."_".$fields[$j]] = $db->f($joinTables[$i]."_".$fields[$j]);
					}
				}
				$returnData[] = $objVal;
			}
		}
		return $returnData;
	}

	public function Push($bannersObj)
	{
		global $id, $name, $description, $image, $popImage, $popWidth, $popHeight, $linkUrl, $urlForView, $urlForClick, $webpageID, $delayPeriod, $startDate, $endDate, $price, $deleteFlag;

		$id = $bannersObj->id;
		$name = $bannersObj->name;
		$description = $bannersObj->description;
		$image = $bannersObj->image;
		$popImage = $bannersObj->popImage;
		$popWidth = $bannersObj->popWidth;
		$popHeight = $bannersObj->popHeight;
		$linkUrl = $bannersObj->linkUrl;
		$urlForView = $bannersObj->urlForView;
		$urlForClick = $bannersObj->urlForClick;
		$webpageID = $bannersObj->webpageID;
		$delayPeriod = $bannersObj->delayPeriod;
		$startDate = $bannersObj->startDate;
		$endDate = $bannersObj->endDate;
		$price = $bannersObj->price;
		$deleteFlag = $bannersObj->deleteFlag;
	}

	public function Pop(&$bannersObj)
	{
		global $id, $name, $description, $image, $popImage, $popWidth, $popHeight, $linkUrl, $urlForView, $urlForClick, $webpageID, $delayPeriod, $startDate, $endDate, $price, $deleteFlag;

		if (isset($id)) $bannersObj->id = $id;
		if (isset($name)) $bannersObj->name = $name;
		if (isset($description)) $bannersObj->description = $description;
		if (isset($image)) $bannersObj->image = $image;
		if (isset($popImage)) $bannersObj->popImage = $popImage;
		if (isset($popWidth)) $bannersObj->popWidth = $popWidth;
		if (isset($popHeight)) $bannersObj->popHeight = $popHeight;
		if (isset($linkUrl)) $bannersObj->linkUrl = $linkUrl;
		if (isset($urlForView)) $bannersObj->urlForView = $urlForView;
		if (isset($urlForClick)) $bannersObj->urlForClick = $urlForClick;
		if (isset($webpageID)) $bannersObj->webpageID = $webpageID;
		if (isset($delayPeriod)) $bannersObj->delayPeriod = $delayPeriod;
		if (isset($startDate)) $bannersObj->startDate = $startDate;
		if (isset($endDate)) $bannersObj->endDate = $endDate;
		if (isset($price)) $bannersObj->price = $price;
		if (isset($deleteFlag)) $bannersObj->deleteFlag = $deleteFlag;
	}

} 
?>